package com.lizardmind.haofudoctor.ui.diagnosis.visit

import android.content.Intent
import android.os.Bundle
import android.view.View
import android.widget.LinearLayout
import com.blankj.utilcode.util.SizeUtils
import com.linxiao.framework.activity.BaseActivity
import com.linxiao.framework.util.color
import com.linxiao.framework.util.getDataBinding
import com.lizardmind.haofudoctor.R
import com.lizardmind.haofudoctor.databinding.ActivityLookVisitBinding
import com.lizardmind.haofudoctor.manager.ImgManager
import com.lizardmind.haofudoctor.manager.postApi
import com.lizardmind.haofudoctor.manager.setImageUrl
import com.lizardmind.haofudoctor.other.DoctorInfo
import com.lizardmind.haofudoctor.other.KaiFangFinishEvent
import com.lizardmind.haofudoctor.other.Request
import com.lizardmind.haofudoctor.other.VisitBean
import com.lizardmind.haofudoctor.ui.diagnosis.sickMan.advisory.kaifang.KaiFangActivity
import com.lizardmind.haofudoctor.ui.other.EvaluationActivity
import com.luck.picture.lib.PictureSelector
import com.luck.picture.lib.config.PictureMimeType
import com.luck.picture.lib.entity.LocalMedia
import io.reactivex.functions.Consumer
import kotlinx.android.synthetic.main.include_title_text.*
import org.greenrobot.eventbus.Subscribe
import org.jetbrains.anko.backgroundColor
import org.jetbrains.anko.imageView
import org.jetbrains.anko.sdk25.coroutines.onClick
import org.jetbrains.anko.startActivity
import org.jetbrains.anko.toast

/**
 * 查看复诊单信息
 * Created by Extends on 2017/11/20 14:59
 */
class LookVisitActivity:BaseActivity() {
    private val binding by lazy { getDataBinding<ActivityLookVisitBinding>(R.layout.activity_look_visit) }
    private val id by lazy { intent.getStringExtra("id") }
    override fun onCreatRootView(): View = binding.root

    override fun onInitView(savedInstanceState: Bundle?) {
        setEvent(true)
        //初始化标题栏
        imgLeft.setImageResource(R.mipmap.back)
        imgLeft.onClick { finish() }
        titleText.text = "复诊单"

        binding.apply {
            alvPrescribing.onClick {
                //开方
                startActivity<KaiFangActivity>("userBean" to bean!!.fill_user,"father_id" to bean!!.id,"father_type" to "2","uid" to bean!!.user_info.uid)
            }
            alvSaveNote.onClick {
                if(note==bean?.note){
                    toast("备注没有改变，不需要保存")
                }else{
                    observe {
                        postApi<Request<String>>(hashMapOf(
                                "action" to "save_visit_note",
                                "token" to DoctorInfo.token,
                                "id" to bean!!.id,
                                "note" to note!!
                        ), Consumer {
                            it.dispose {
                                toast(it)
                            }
                        })
                    }
                }
            }
        }


        observe {
            postApi<Request<VisitBean>>(hashMapOf(
                    "action" to "visit_info",
                    "token" to DoctorInfo.token,
                    "id" to id
            ), Consumer {
                it.dispose {
                    binding.bean = it
                    initView()
                }
            },context = this)
        }
    }

    fun initView(){
        binding.apply {
            if(bean?.is_prescribing==0){
                alvPrescribing.isEnabled = true
                alvPrescribing.backgroundColor = color(R.color.notice_red)
                alvPrescribing.text = "为TA开方"
            }else{
                alvPrescribing.isEnabled = false
                alvPrescribing.backgroundColor = color(R.color.unChecked)
                alvPrescribing.text = "已开方"
            }
            note = bean?.note
            val lp = LinearLayout.LayoutParams(SizeUtils.dp2px(57f),SizeUtils.dp2px(57f))
            lp.marginStart = SizeUtils.dp2px(5f)
            alvTongue.apply {
                val list = bean!!.tongue_picture.map { LocalMedia(ImgManager.IMGURL+it,0, PictureMimeType.ofImage(),PictureMimeType.JPEG) }
                bean!!.tongue_picture.forEachIndexed { i,it->
                    imageView {
                        layoutParams = lp
                        setImageUrl(it)
                        onClick {
                            PictureSelector.create(this@LookVisitActivity).externalPicturePreview(i,list)
                        }
                    }
                }
            }
        }

    }

    @Subscribe
    fun getFinishInform(event: KaiFangFinishEvent){
        binding.alvPrescribing.isEnabled = false
        binding.alvPrescribing.backgroundColor = color(R.color.unChecked)
        binding.alvPrescribing.text = "已开方"
    }
}